<template>
  <el-card>
    <el-form :model="space" label-width="120px" ref="form">
      <el-form-item label="广告位名称" prop="name" required>
        <el-input v-model="space.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSave">保存</el-button>
        <el-button v-if="!isEdit" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
import Vue from 'vue'
import { Form } from 'element-ui'
import { saveOrUpdate, getSpaceById } from '@/services/advert-space'

export default Vue.extend({
  name: 'CreateOrUpdateAdvert',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    spaceId: {
      type: [String, Number]
    }
  },
  data () {
    return {
      space: {
        name: ''
      }
    }
  },
  created () {
    if (this.isEdit) {
      this.loadSpace()
    }
  },
  methods: {
    async loadSpace () {
      const { data } = await getSpaceById(this.spaceId)
      this.space = data.content
    },

    async handleSave () {
      const { data } = await saveOrUpdate(this.space)
      if (data.success) {
        this.$message.success('保存成功')
        this.$router.push('/advert-space')
      } else {
        this.$message.error('保存失败')
      }
    },

    resetForm () {
      (this.$refs.form as Form).resetFields()
    }
  }
})
</script>

<style lang="scss" scoped>
.el-step {
  cursor: pointer;
}
</style>
